﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;

namespace SPSolutions.Samples.RibbonApp.WebControls
{
    /// <summary>
    /// Represent a ribbon script manager
    /// </summary>
	public class RibbonScriptManager
	{
        // Fields
        private string m_scriptPath;

        public RibbonScriptManager(string scriptPath)
        {
            m_scriptPath = scriptPath;
        }

        /// <summary>
        /// Get the script path
        /// </summary>
        public string ScriptPath
        {
            get { return m_scriptPath; }
        }

        /// <summary>
        /// Register the ribbon initialization function
        /// </summary>
        /// <param name="control"></param>
        /// <param name="functionName"></param>
        /// <param name="scriptName"></param>
        /// <param name="localizable"></param>
        /// <param name="initializeStatement"></param>
        public void RegisterInitializeFunction(System.Web.UI.Control control, string functionName, string scriptName, bool localizable, string initializeStatement)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            if (string.IsNullOrEmpty(functionName))
            {
                throw new ArgumentNullException("functionName");
            }
            if (string.IsNullOrEmpty(scriptName))
            {
                throw new ArgumentNullException("scriptPath");
            }
            if (string.IsNullOrEmpty(initializeStatement))
            {
                throw new ArgumentNullException("initializeStatement");
            }

            // Register required scripts
            ScriptLink.RegisterScriptAfterUI(control.Page, "CUI.js", false, true);
            ScriptLink.RegisterScriptAfterUI(control.Page, "SP.Ribbon.js", false, true);
            ScriptLink.RegisterScriptAfterUI(control.Page, ScriptPath + scriptName, localizable, true);
            
            // Create a string builder
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

            // Create initialization script
            stringBuilder.Append("<script type=\"text/javascript\" defer=\"true\">\n");
            stringBuilder.Append("//<![CDATA[\n");
            stringBuilder.Append("function " + functionName + "() {\n");
            stringBuilder.Append(initializeStatement + ";\n");
            stringBuilder.Append("}\n");

            // TODO: Modify code to ensure loading of the script before calling function
            stringBuilder.Append("ExecuteOrDelayUntilScriptLoaded(" + functionName + ", '" + scriptName + "');\n");
            stringBuilder.Append("//]]\n");
            stringBuilder.Append("</script>\n");

            // Register the client script block
            control.Page.ClientScript.RegisterClientScriptBlock(control.GetType(), functionName, stringBuilder.ToString(), false);
        }
	}
}
